package com.qdxwx.manage.controller.member;

import com.qdxwx.common.json.AjaxResult;
import com.qdxwx.common.page.PageRequest;
import com.qdxwx.common.page.TableDataInfo;
import com.qdxwx.common.utils.TKConvert;
import com.qdxwx.common.utils.TKString;
import com.qdxwx.data.service.member.MemberRecordService;
import com.qdxwx.data.service.member.MemberTypeService;
import com.qdxwx.data.service.order.OrderDetailService;
import com.qdxwx.data.service.user.UserLetterService;
import com.qdxwx.data.service.user.UserService;
import com.qdxwx.manage.controller.BaseController;

import com.qdxwx.manage.support.SessionUtils;
import com.qdxwx.models.enums.LetterTemplate;
import com.qdxwx.models.manager.Manager;
import com.qdxwx.models.member.MemberRecord;
import com.qdxwx.models.member.MemberRecordVO;
import com.qdxwx.models.member.MemberType;
import com.qdxwx.models.order.OrderDetail;
import com.qdxwx.models.user.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import jakarta.servlet.http.HttpServletRequest;
import java.time.LocalDate;
import java.util.List;

/**
 * 会员开通记录Controller
 * Qdx NetSchool Platform
 * @author qdxwx
 * @updateTime 2022/5/25
 * @description
 */
@Controller
@RequestMapping("/member/record")
public class MemberRecordController extends BaseController {

    @Autowired
    private MemberRecordService memberRecordService;
    @Autowired
    private MemberTypeService memberTypeService;
    @Autowired
    private OrderDetailService orderDetailService;
    @Autowired
    private UserLetterService userLetterService;
    @Autowired
    private UserService userService;

    @GetMapping("/list.html")
    public String list(ModelMap mmap) {
        MemberType memberType = new MemberType();
        memberType.setStatus(0);
        List<MemberType> list = memberTypeService.getList(memberType, null);
        mmap.put("memberType", list);
        return "member/member-record-list";
    }

    /**
     * 查询会员开通记录列表
     */
    @PostMapping("/list.json")
    @ResponseBody
    public TableDataInfo list(HttpServletRequest request) {
        String mobile = request.getParameter("mobile");
        String userName = TKString.trim(request.getParameter("userName"));
        MemberRecord memberRecord = new MemberRecord();

        if (TKString.isMobile(mobile)) {
            User user = userService.getByMobile(mobile);
            if (user != null) {
                memberRecord.setUserId(user.getUserId());
            }
        }
        else if (userName.length() > 1){
            User user = userService.getByLoginName(userName);
            if (user != null) {
                memberRecord.setUserId(user.getUserId());
            }
        }
        memberRecord.setTypeId(TKConvert.toInteger(request.getParameter("memberType")));
        List<MemberRecordVO> list = memberRecordService.getVOList(memberRecord, PageRequest.form(request));
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(0);
        rspData.setRows(list);
        rspData.setTotal(memberRecordService.count(memberRecord));
        return rspData;
    }

    /**
     * 开通详情
     */
    @GetMapping("/delay.html")
    public String Info(HttpServletRequest request, ModelMap mmap) {
        long id = TKConvert.toLong(request.getParameter("id"));
        if (id < 1) return error404;
        MemberRecord memberRecord = memberRecordService.get(id);
        if (memberRecord == null) return error500;
        mmap.put("memberRecord", memberRecord);
        return "member/member-record-delay";
    }

    /**
     * 会员详情记录延迟过期时间操作
     */
    @PostMapping("/delay.json")
    @ResponseBody
    public AjaxResult ClickStopTIme(HttpServletRequest request) {
        Manager manager = SessionUtils.getManager();
        if (manager == null){
            return AjaxResult.error("登录信息异常！");
        }
        long id = TKConvert.toLong(request.getParameter("id"));
        if (id < 1) return AjaxResult.error("参数id异常！");
        LocalDate stopTime = TKConvert.toLocalDate(request.getParameter("stopTime"));
        if (LocalDate.now().isAfter(stopTime)){
            return AjaxResult.error("延期时间必须大于原来的到期日期");
        }
        MemberRecord memberRecord = memberRecordService.get(id);
        if (memberRecord==null) {
            return AjaxResult.error("会员信息错误！");
        }
        memberRecord.setEndDate(stopTime);
        memberRecordService.updateEndDate(memberRecord, manager);
        OrderDetail orderDetail = orderDetailService.getByOrderId(memberRecord.getOrderId());
        if (orderDetail == null) return AjaxResult.error("对应订单信息错误！");
        orderDetail.setAuthDate(stopTime);
        int row = orderDetailService.update(orderDetail);
        if (row > 0) {
            userLetterService.send(orderDetail.getUserId(), LetterTemplate.ORDER_DELAY, orderDetail.getCourseName());

        }
        return toAjax(row);
    }

}
